package leetcode2

type ListNode struct {
	Val  int
	Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil {
		return l2
	} else if l2 == nil {
		return l1
	}
	head := &ListNode{}
	cur := head
	temp := 0
	for {
		if l1 == nil && l2 == nil {
			if temp > 0 {
				cur.Next = &ListNode{
					Val: temp % 10,
				}
			}
			break
		}
		if l1 != nil {
			temp += l1.Val
			l1 = l1.Next
		}
		if l2 != nil {
			temp += l2.Val
			l2 = l2.Next
		}
		cur.Next = &ListNode{
			Val: temp % 10,
		}
		temp = temp / 10
		cur = cur.Next
	}
	return head.Next
}

func makeList(num int) *ListNode {
	res := make([]int, 0)

	for {
		parent := num / 10
		cur := num % 10
		res = append(res, cur)
		if parent > 0 {
			break
		}
		num = parent
	}
	head := &ListNode{}
	cur := head
	for i := len(res) - 1; i >= 0; i-- {
		cur.Next = &ListNode{
			Val: res[i],
		}
		cur = cur.Next
	}
	return head.Next
}
